<template>
    <view>
        <view class="headtou" >
            <view class="head" >
                <view style="width: 30%">
                    <text></text>
                </view>
                <view class="headimgtest">
                    <image style="height: 70rpx; width: 70rpx" src="https://xxzhang.oss-cn-hangzhou.aliyuncs.com/%E7%9A%87%E5%86%A0.png"></image>
                    <view class="himg">
                        <image :src="imageUrl" style="height: 100rpx; width: 100rpx"></image>
                    </view>
                    <text style="margin-top: 10rpx">{{ nickname }}</text>
                    <text style="margin-top: 10rpx; font-size: 26rpx">{{ area }}</text>
                </view>
                <view class="jinpai" style="width: 30%">
                    <image style="width: 70rpx; height: 75rpx; margin-left: 150rpx" src="https://xxzhang.oss-cn-hangzhou.aliyuncs.com/%E9%87%91%E7%89%8C%20%281%29.png"></image>
                </view>
            </view>
			
			<view style="display: flex;justify-content: center;margin-top: 16rpx;">
				<view v-if="isSupportMonthly == 1">
					<view v-if="isMonthly == 0" @tap="baoyue" style="border-radius: 50rpx;background-color: #5ac724; width: 280rpx; height: 60rpx;display: flex;justify-content: center;align-items: center;">
						<text style="color: white;font-size: 25rpx">五年级包月{{monthlySubscriptionMoney}}.00元</text>
					</view>
					<view v-if="isMonthly == 1"  style="border-radius: 50rpx; width: 280rpx; height: 60rpx;display: flex;justify-content: center;align-items: center;">
						<image src="../../static/iconfont/yibaoyue.png" style="height: 45rpx;width: 45rpx;"></image>
						<text style="color: #00bc00;font-size: 25rpx;margin-left: 16rpx;font-weight: bold;">已包月</text>
					</view>
				</view>
				<view v-if="isSupportMonthly == 0">
					<view  style="border-radius: 50rpx;background-color: #5ac724; width: 280rpx; height: 60rpx;display: flex;justify-content: center;align-items: center;">
						<text style="color: white;font-size: 25rpx">该讲师暂不支持包月</text>
					</view>
				</view>
				
			</view>

            <view class="list">
                <view class="dianzan">
                    <image @tap="dianzanjs" style="height: 50rpx; width: 50rpx" src="/static/iconfont/dz.png"></image>
                    <view>
                        <text style="font-size: 27rpx">{{ dianzhanNum }}</text>
                        <text style="font-size: 27rpx; color: #949897; margin-left: 10rpx">点赞</text>
                    </view>
                </view>
                <view class="dianzan">
                    <image
                        @tap="guanzhujs"
                        style="height: 50rpx; width: 50rpx"
                        src="https://xxzhang.oss-cn-hangzhou.aliyuncs.com/icon-%E5%85%B3%E6%B3%A8%E5%B7%B2%E5%85%B3%E6%B3%A8.png"
                    ></image>
                    <view>
                        <text style="font-size: 27rpx">{{ guangzhuNum }}</text>
                        <text style="font-size: 27rpx; color: #949897; margin-left: 10rpx">关注</text>
                    </view>
                </view>
                <view class="dianzan">
                    <image @tap="requestingUpdates" :id="id" style="height: 50rpx; width: 50rpx" src="https://xxzhang.oss-cn-hangzhou.aliyuncs.com/%E7%82%B9%E5%87%BB.png"></image>
                    <view>
                        <text style="font-size: 27rpx; color: #949897">求更新</text>
                    </view>
                </view>
				<view class="dianzan">
				    <image @tap="daShang" :id="id" style="height: 50rpx; width: 40rpx" src="/static/iconfont/dashang.png"></image>
				    <view>
				        <text style="font-size: 27rpx; color: #949897">打赏</text>
				    </view>
				</view>
            </view>
        </view>

        <view class="gongzonghaotupian">
            <block v-for="(item, index) in gongzonghao" :key="index">
                <view class="gongzh">
                    <image show-menu-by-longpress="ture" :src="item.imgUrl" style="height: 160rpx; width: 160rpx"></image>
                    <text style="color: rgb(248, 87, 87); font-size: 30rpx">{{ item.title }}</text>
                </view>
            </block>
        </view>

        <view class="hx"></view>

        <view class="case-display">
            <block v-for="(item, index) in navigationList" :key="index">
                <view :class="currentId == item.id ? 'active' : 'default'" @tap="selShopTc" :id="item.id">
                    <text>{{ item.name }}</text>
                </view>
            </block>
        </view>

        <view class="appointment" v-if="courseware.length != 0">
            <block v-for="(courseware, index) in courseware" :key="index">
                <navigator url="url" :id="courseware.id" @tap="details" class="mod-item">
                    <view class="mod-signup">
                        <image style="width: 50rpx; height: 50rpx" src="https://xxzhang.oss-cn-hangzhou.aliyuncs.com/%E9%87%91%E7%89%8C%20%281%29.png"></image>
                    </view>
                    <view class="toubu">
                        <view class="liebiao">
                            <view class="Limgs">
                                <image :src="imageUrl" style="height: 80rpx; width: 80rpx"></image>
                            </view>
                            <text style="font-weight: bold; font-size: 30rpx; margin-top: 10rpx">{{ nickname }}</text>
                        </view>
                    </view>
                    <view class="nianjiAndtime">
                        <view class="nianji">
                            <view v-if="courseware.classification == 5" class="type">
                                <text style="font-size: 25rpx;color: #949897">{{ courseware.classificationName }}</text>
								<text v-if="courseware.type == 1 && courseware.whichIssue == 2" style="font-size: 25rpx;color: #ebba57;font-weight:bold;">(收费)</text>
								<text v-if="courseware.type == 1 && courseware.whichIssue == 1" style="font-size: 25rpx;color: #397433;font-weight:bold;">(免费)</text>
								<text v-if="courseware.type == 2 && courseware.whichIssue == 2" style="font-size: 25rpx;color: #ebba57;font-weight:bold;">(收费)</text>
								<text v-if="courseware.type == 2 && courseware.whichIssue == 1" style="font-size: 25rpx;color: #397433;font-weight:bold;">(免费)</text>
								
                            </view>
                            <view v-if="courseware.classification == 7" class="type">
                                <text style="font-size: 25rpx;color: #949897">{{ courseware.classificationName }}</text>
								<text v-if="courseware.type == 1 && courseware.whichIssue == 2" style="font-size: 25rpx;color: #ebba57;font-weight:bold;">(收费)</text>
								<text v-if="courseware.type == 1 && courseware.whichIssue == 1" style="font-size: 25rpx;color: #397433;font-weight:bold;">(免费)</text>
								<text v-if="courseware.type == 2 && courseware.whichIssue == 2" style="font-size: 25rpx;color: #ebba57;font-weight:bold;">(收费)</text>
								<text v-if="courseware.type == 2 && courseware.whichIssue == 1" style="font-size: 25rpx;color: #397433;font-weight:bold;">(免费)</text>
																
                            </view>
                            <view v-if="courseware.classification == 9" class="type">
                                <text style="font-size: 25rpx;color: #949897">{{ courseware.classificationName }}</text>
								<text style="font-size: 25rpx;color: #397433;font-weight:bold;">(免费)</text>
								
                            </view>
                            <view v-if="courseware.classification == 11" class="type">
                                <text style="font-size: 25rpx;color: #949897">{{ courseware.classificationName }}</text>
								<text style="font-size: 25rpx;color: #397433;font-weight:bold;">(免费)</text>
                            </view>
                        </view>
                        <view class="time">
                            <text style="font-size: 27rpx; color: #949897">{{ courseware.addTime }}</text>
                        </view>
                    </view>

                    <view class="xhx"></view>

                    <view class="title">
                        <text style="font-size: 30rpx">{{ courseware.title }}</text>
                    </view>

                    <view class="anniu">
                        <view class="chakan" v-if="courseware.type == 2">
                            <text style="color: white; font-size: 25rpx">查看图片</text>
                        </view>

                        <view class="chakansp" v-if="courseware.type == 1 && courseware.whichIssue == 1">
                            <text  style="color: white; font-size: 25rpx">查看视频</text>
                        </view>
						<view class="chakansp2" v-if="courseware.type == 1 && courseware.whichIssue == 2">
						    <text  style="color: white; font-size: 25rpx">支付￥10.00</text>
						</view>
                    </view>
                </navigator>
            </block>
        </view>

        <view v-if="courseware.length == 0">
            <view class="wushuju">
                <image style="width: 100rpx; height: 70rpx" src="https://xxzhang.oss-cn-hangzhou.aliyuncs.com/%E6%97%A0%E6%95%B0%E6%8D%AE%20%281%29.png"></image>
                <text style="color: darkgrey">暂无数据</text>
            </view>
        </view>
    </view>
</template>

<script>
const app = getApp();
var urlPrefix = app.globalData.urlPrefix;
export default {
    data() {
        return {
			isSupportMonthly:0,
			isMonthly:0,
			monthlySubscriptionMoney:0,
            userId: null,
            dianzhanNum: 0,
            guangzhuNum: 0,
            imageUrl: '',
            nickname: '',
            area: '',
            courseware: [],
            id: null,
            currentId: 1,
            navigationList: [],
            gongzonghao: [],
			token:null
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad(options) {
		uni.showLoading({
		      title: '加载中',
		});
        let that = this;
        var userId = uni.getStorageSync('userId');
		var token = uni.getStorageSync('token');
		this.token = token;
        that.setData({
            id: options.id,
            userId: userId
        });
        uni.request({
            url: urlPrefix + '/api/applet/index/searchList',
            method: 'GET',
            header: {
                'content-type': 'application/json',
                 'token': this.token
            },
            success: function (res) {
                let dataList = res.data;
                that.setData({
                    navigationList: dataList.datas
                });
            }
        });

        /**
         * 初始化课件列表
         */
        uni.request({
            url: urlPrefix + '/api/applet/tLecturer/myLecturerVideo?userId=' + options.id + '&type=1',
            data: {},
            method: 'GET',
            header: {
                'content-type': 'application/json',
               'token': this.token
            },
            success: function (res) {
				uni.hideLoading();
				
                let dataList = res.data;
                that.setData({
                    courseware: dataList.datas
                });
            }
        });

        /**
         * 根据id获取讲师详情
         */
        uni.request({
            url: urlPrefix + '/api/applet/userToLecturer/lecturerById?id=' + options.id + '&userId=' + userId,
            method: 'GET',
            header: {
                'content-type': 'application/json',
                  'token': this.token
            },
            success: function (res) {
                let dataList = res.data;
                that.setData({
                    imageUrl: dataList.datas.headImgUrl,
                    nickname: dataList.datas.name,
                    dianzhanNum: dataList.datas.dianzhanNum,
                    guangzhuNum: dataList.datas.guangzhuNum,
                    area: dataList.datas.province + '-' + dataList.datas.city,
					isMonthly:dataList.datas.isMonthly,
					isSupportMonthly:dataList.datas.isSupportMonthly,
					monthlySubscriptionMoney:dataList.datas.monthlySubscriptionMoney
                });
            }
        });

        /**
         * 获取公众号二维码
         */
        uni.request({
            url: urlPrefix + '/api/applet/tWechatOfficialAccount/list',
            method: 'GET',
            header: {
                'content-type': 'application/json',
                  'token': this.token
            },
            success: function (res) {
                let dataList = res.data;
                if (dataList.resp_code == 0) {
                    that.setData({
                        gongzonghao: dataList.datas
                    });
                }
            }
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
		 let that = this;
		/**
		 * 初始化课件列表
		 */
		uni.request({
		    url: urlPrefix + '/api/applet/tLecturer/myLecturerVideo?userId=' + this.id + '&type=1',
		    data: {},
		    method: 'GET',
		    header: {
		        'content-type': 'application/json',
		       'token': this.token
		    },
		    success: function (res) {
				uni.hideLoading();
				
		        let dataList = res.data;
		        that.setData({
		            courseware: dataList.datas
		        });
		    }
		});
		uni.request({
		    url: urlPrefix + '/api/applet/userToLecturer/lecturerById?id=' + this.id + '&userId=' + this.userId,
		    method: 'GET',
		    header: {
		        'content-type': 'application/json',
		          'token': this.token
		    },
		    success: function (res) {
		        let dataList = res.data;
		        that.setData({
		            imageUrl: dataList.datas.headImgUrl,
		            nickname: dataList.datas.name,
		            dianzhanNum: dataList.datas.dianzhanNum,
		            guangzhuNum: dataList.datas.guangzhuNum,
		            area: dataList.datas.province + '-' + dataList.datas.city,
					isMonthly:dataList.datas.isMonthly,
					isSupportMonthly:dataList.datas.isSupportMonthly,
					monthlySubscriptionMoney:dataList.datas.monthlySubscriptionMoney
		        });
		    }
		});
	
		uni.stopPullDownRefresh();  //停止刷新            
			
	},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {},
    onShareTimeline(res) {
        // 分享到朋友圈
        return {};
    },
    methods: {
		baoyue:function(res){
			
			uni.showLoading({
			        title: '订单创建中',
					mask:true
								
			});
			
			
			let that = this;
			 var userId = uni.getStorageSync('userId');
			 uni.request({
			     url: urlPrefix + '/api/app/recharge/monthlySubscription?lid=' + this.id + '&userId=' + userId + "&money="+this.monthlySubscriptionMoney,
			     method: 'GET',
			     header: {
			         'content-type': 'application/json',
			         'token': uni.getStorageSync('token')
			     },
			     success: function (res) {
			         if (res.data.resp_code == 0) {
						 
			 		        uni.hideLoading();
					   
							uni.requestPayment({	
								
							     "provider": "wxpay", 
								 "orderInfo": {
									 "appid":  res.data.datas.appid,  // 微信开放平台 - 应用 - AppId，注意和微信小程序、公众号 AppId 可能不一致
									 "noncestr":  res.data.datas.noncestr, // 随机字符串
									 "package": "Sign=WXPay",        // 固定值
									 "partnerid": res.data.datas.partnerid,      // 微信支付商户号
									 "prepayid": res.data.datas.prepayid, // 统一下单订单号 
									 "timestamp":res.data.datas.timestamp,        // 时间戳（单位：秒）
									 "sign": res.data.datas.sign// 签名，这里用的 MD5/RSA 签名
								 },
								 success(res) {
									 that.setData({
										 isMonthly : 1
									 });
									
								 },
								 fail(e) {
									 uni.showToast({
									     title: '支付失败',
									     icon: 'none',
									     duration: 2000
									 });
								 }
							})
					
			           
			         }else {
			 			uni.showToast({
			 			    title: res.data.resp_msg,
			 			    icon: 'none',
			 			    duration: 2000
			 			});
			 		}
			     
				}
			});
		},
		
        //跳转可将详情
        details: function (res) {
            var id = res.currentTarget.id;
            uni.navigateTo({
                url: '/pages/coursewaredetails/coursewaredetails?id=' + id
            });
        },

        //点击导航查看课件列表
        selShopTc: function (res) {
			uni.showLoading({
			      title: '加载中',
			});
            let that = this;
            uni.request({
                url: urlPrefix + '/api/applet/tLecturer/myLecturerVideo?userId=' + this.id + '&type=' + res.currentTarget.id,
                method: 'GET',
                header: {
                    'content-type': 'application/json',
                      'token': this.token
                },
                success: function (suss) {
                    let dataList = suss.data;
					uni.hideLoading();
                    that.setData({
                        currentId: res.currentTarget.id,
                        courseware: dataList.datas
                    });
                }
            });
        },

        //点赞/取消点赞
        dianzanjs: function (res) {
            let that = this;
            uni.request({
                url: urlPrefix + '/api/applet/userToLecturer/like?userId=' + this.userId + '&id=' + this.id,
                method: 'GET',
                header: {
                    'content-type': 'application/json',
                      'token': this.token
                },
                success: function (suss) {
                    let dataList = suss.data;
                    that.setData({
                        dianzhanNum: dataList.datas
                    });
                }
            });
        },

        //关注/取消关注
        guanzhujs: function (res) {
            let that = this;
            uni.request({
                url: urlPrefix + '/api/applet/userToLecturer/follow?userId=' + this.userId + '&id=' + this.id,
                method: 'GET',
                header: {
                    'content-type': 'application/json',
                      'token': this.token
                },
                success: function (suss) {
                    let dataList = suss.data;
                    that.setData({
                        guangzhuNum: dataList.datas
                    });
                }
            });
        },
		
		isMoney(value) {
		  // 正则表达式，匹配金额格式，包括小数点和负数
		  const regex = /^[1-9]\d*$/;
		  return regex.test(value);
		},
		
		daShang: function (res) {
			let that = this;
			uni.showModal({
						title: '请输入打赏金额',
						content: '',
						editable: true, //是否显示输入框
						placeholderText: '1到100元(优先使用余额)', //输入框提示内容
						confirmText: '确认',
						cancelText: '取消',
						success: (res) => {
							if (res.confirm) {
							  if(res.content <1 || res.content >100){
								  uni.showToast({
									  title: '只能打赏1到100元',
									  icon: 'none',
									  duration: 2000
								  });
								  return;
							  }
							 var a = that.isMoney(res.content);
							
							 
							  if(!that.isMoney(res.content)){
								  uni.showToast({
									  title: '请输入整数金额',
									  icon: 'none',
									  duration: 2000
								  });
								  return;
							  }
							  
							  let jine = res.content
							  uni.showModal({
							      title: '操作提示',
							      content: '是否确认打赏该讲师?',
							      success(res) {
									
							          if (res.confirm) {
							              uni.request({
							                 url: urlPrefix + '/api/applet/myInfo/myAmt?userId=' + that.userId,
							                  method: 'GET',
							                  header: {
							                      'content-type': 'application/json',
							                      'token': uni.getStorageSync('token')
							                  },
							                  success: function (suss) {
							                      if (suss.data.datas >= jine) {
							                         uni.request({ 
														  url: urlPrefix + '/api/applet/myInfo/daShang?userId=' + that.userId+"&lecturerId="+that.id+"&money="+jine,
														  method: 'GET',
														  header: {
														      'content-type': 'application/json',
														      'token': uni.getStorageSync('token')
														  },
														   success: function (e) {
															   uni.showToast({
															       title: e.data.resp_msg,
															       icon: 'none',
															       duration: 2000
															   });
														   }
													 });
							                      } else {
							                         uni.request({
							                              url: urlPrefix + '/api/app/recharge/weChatRewards?lecturerId=' + that.id + '&userId=' + that.userId + "&money="+jine,
							                              method: 'GET',
							                              header: {
							                                  'content-type': 'application/json',
							                                  'token': uni.getStorageSync('token')
							                              },
							                              success: function (res) {
							                                  if (res.data.resp_code == 0) {
							                         			 
							                          		        uni.hideLoading();
							                         		   
							                         				uni.requestPayment({	
							                         					
							                         				     "provider": "wxpay", 
							                         					 "orderInfo": {
							                         						 "appid":  res.data.datas.appid,  // 微信开放平台 - 应用 - AppId，注意和微信小程序、公众号 AppId 可能不一致
							                         						 "noncestr":  res.data.datas.noncestr, // 随机字符串
							                         						 "package": "Sign=WXPay",        // 固定值
							                         						 "partnerid": res.data.datas.partnerid,      // 微信支付商户号
							                         						 "prepayid": res.data.datas.prepayid, // 统一下单订单号 
							                         						 "timestamp":res.data.datas.timestamp,        // 时间戳（单位：秒）
							                         						 "sign": res.data.datas.sign// 签名，这里用的 MD5/RSA 签名
							                         					 },
							                         					 success(res) {
							                         						uni.showToast({
							                         						    title: '打赏成功',
							                         						    icon: 'none',
							                         						    duration: 2000
							                         						});
							                         					 },
							                         					 fail(e) {
							                         						 uni.showToast({
							                         						     title: '打赏失败',
							                         						     icon: 'none',
							                         						     duration: 2000
							                         						 });
							                         					 }
							                         				})
							                         		
							                                    
							                                  }else {
							                          			uni.showToast({
							                          			    title: res.data.resp_msg,
							                          			    icon: 'none',
							                          			    duration: 2000
							                          			});
							                          		}
							                              
							                         	}
							                         });
							                      }
							                  }
							              });
							          } else if (res.cancel) {
							              //这里是点击取消执行事件
							          }
							      }
							  });
							}
						}
					});
		},

        //催更
        requestingUpdates: function (r) {
            let id = r.currentTarget.id;
            var userId = uni.getStorageSync('userId');
            uni.showModal({
                title: '操作提示',
                content: '是否向讲师发送更新请求?',
                success(res) {
                    if (res.confirm) {
                        uni.request({
                            url: urlPrefix + '/api/applet/userToLecturer/requestingUpdates?userId=' + userId + '&id=' + id,
                            method: 'GET',
                            header: {
                                'content-type': 'application/json',
                                  'token': this.token
                            },
                            success: function (suss) {
                                if (suss.data.resp_code == 0) {
                                    uni.showToast({
                                        title: '提交成功',
                                        icon: 'success',
                                        duration: 2000
                                    });
                                } else {
                                    uni.showToast({
                                        title: '已提交，请勿重复操作',
                                        icon: 'none',
                                        duration: 2000
                                    });
                                }
                            }
                        });
                    } else if (res.cancel) {
                        //这里是点击取消执行事件
                    }
                }
            });
        }
    }
};
</script>
<style lang="scss">
@import './courseware.scss';
</style>
